//
//  Parameters.swift
//  SwiftSign
//
//  Created by continue on 2021/5/6.
//

import Foundation
import UIKit
import AdSupport
import SystemConfiguration.CaptiveNetwork

class Parameters {
    
    public func sort(params: [String : String]) -> String {
        let keys = params.keys.sorted()
        var arr = [String]()
        for key in keys {
            let value = "\(key)=\(params[key]!)"
            arr.append(value)
        }
        let all = arr.joined(separator: "&")
        return all
    }
    
    public func getCommonParameters() -> [String : String] {
        let parameters = ["game_id": Configs.App.gameId,
                          "game_pkg" : Configs.App.gamePKG,
                          "install" : getInstallState(),
                          "sdk_ver" : Configs.App.SDKVersion,
                          "partner_id" : Configs.App.partnerId,
                          "net_type" : "wifi",
                          "dname" : iphoneType(),
                          "device" : Configs.App.device,
                          "uuid" : IDFA(),
                          "build_ver" : Configs.App.buildVersion,
                          "mac" : macAddress(),
                          "idfv" : UDID(),
                          "wifi" : SSID(),
                          "game_ver": bundleVersion(),
                          "time" : timeInterval()]
        return parameters
    }
    
    public func iphoneType() -> String {

        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        return identifier as String
    }

    public func macAddress() -> String {
        return "02:00:00:00:00:00"
    }

    public func SSID() -> String {
        var ssid: String = "unknown"
        if let interfaces = CNCopySupportedInterfaces() as NSArray? {
            for interface in interfaces {
                if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                    ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String ?? "unknown"
                    break
                }
            }
        }
        return ssid
    }

    public func UDID() -> String {
        return UIDevice.current.identifierForVendor!.uuidString
    }

    public func IDFA() -> String {
        return ASIdentifierManager.shared().advertisingIdentifier.uuidString
    }

    public func systemVersion() -> String {
       return UIDevice.current.systemVersion
    }

    public func bundleVersion() -> String {
        let infoDictionary = Bundle.main.infoDictionary
        let shortVersion = infoDictionary!["CFBundleShortVersionString"]
        return shortVersion as! String
    }

    public func timeInterval() -> String {
        let date = Date()
        let timeZone = NSTimeZone.system
        let seconds = timeZone.secondsFromGMT(for: date)
        let locaDate = date.addingTimeInterval(TimeInterval(seconds))
        let time = locaDate.timeIntervalSince1970
        return String(Int64(time))
    }

    public func getInstallState() -> String {
        let userDefault = UserDefaults.standard
        let valueStr = userDefault.object(forKey: "installIdKey")
        guard valueStr != nil else {
            return "1"
        }
        return valueStr as! String
    }

    public func setInstallState() {
        let userDefault = UserDefaults.standard
        userDefault.set("0", forKey: "installIdKey")
    }

}
